<template>
  <div class="register-container">
    <div class="register-box animate-fade-in">
      <div class="register-logo">
        <el-icon class="logo-icon"><Monitor /></el-icon>
      </div>
      <h2 class="register-title">MyAI 智能问答系统</h2>
      <p class="register-subtitle">创建新账户</p>
      <el-form :model="registerForm" ref="registerFormRef" :rules="registerRules" class="register-form">
        <el-form-item prop="username">
          <el-input 
            v-model="registerForm.username" 
            placeholder="请输入用户名" 
            :prefix-icon="User"
            class="custom-input" />
        </el-form-item>
        <el-form-item prop="password">
          <el-input 
            v-model="registerForm.password" 
            type="password" 
            placeholder="请输入密码" 
            :prefix-icon="Lock"
            show-password
            class="custom-input" />
        </el-form-item>
        <el-form-item prop="confirmPassword">
          <el-input 
            v-model="registerForm.confirmPassword" 
            type="password" 
            placeholder="请确认密码" 
            :prefix-icon="Lock"
            show-password
            class="custom-input" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="register-btn" @click="handleRegister" :loading="loading">
            <span>注册账户</span>
            <el-icon class="el-icon--right"><ArrowRight /></el-icon>
          </el-button>
        </el-form-item>
      </el-form>
      <div class="register-options">
        <span>已有账户？</span>
        <el-button type="text" @click="goToLogin">返回登录</el-button>
      </div>
      <div class="register-footer">
        <p>© 2023 MyAI 智能问答系统</p>
      </div>
    </div>
    <div class="register-decoration">
      <div class="floating-circle circle-1"></div>
      <div class="floating-circle circle-2"></div>
      <div class="floating-circle circle-3"></div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock, Monitor, ArrowRight } from '@element-plus/icons-vue'
import { userApi } from '@/api'

const router = useRouter()
const loading = ref(false)

const registerForm = reactive({
  username: '',
  password: '',
  confirmPassword: ''
})

// 自定义校验规则：确认密码必须和密码一致
const validateConfirmPassword = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请再次输入密码'))
  } else if (value !== registerForm.password) {
    callback(new Error('两次输入密码不一致'))
  } else {
    callback()
  }
}

const registerRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度应为3-20个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于6个字符', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请确认密码', trigger: 'blur' },
    { validator: validateConfirmPassword, trigger: 'blur' }
  ]
}

const registerFormRef = ref(null)

const handleRegister = () => {
  registerFormRef.value.validate((valid) => {
    if (valid) {
      loading.value = true
      // 调用注册API
      userApi.register({
        username: registerForm.username,
        password: registerForm.password,
        rePassword: registerForm.confirmPassword
      }).then(() => {
        ElMessage.success('注册成功，请登录')
        router.push('/login')
      })
        .catch(() => {
          // 错误处理已在request.js中统一处理
        })
        .finally(() => {
          loading.value = false
        })
    } else {
      return false
    }
  })
}

const goToLogin = () => {
  router.push('/login')
}
</script>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-image: url('../assets/images/login-bg.svg');
  background-size: cover;
  background-position: center;
  position: relative;
  overflow: hidden;
}

.register-box {
  width: 440px;
  padding: 40px;
  background-color: rgba(255, 255, 255, 0.95);
  border-radius: 16px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
  z-index: 10;
  text-align: center;
}

.register-logo {
  margin-bottom: 20px;
}

.logo-icon {
  font-size: 48px;
  color: #409EFF;
}

.register-title {
  font-size: 24px;
  font-weight: 600;
  color: #303133;
  margin-bottom: 8px;
}

.register-subtitle {
  font-size: 16px;
  color: #606266;
  margin-bottom: 30px;
}

.register-form {
  margin-bottom: 20px;
}

.custom-input {
  height: 50px;
}

.register-btn {
  width: 100%;
  height: 50px;
  font-size: 16px;
  font-weight: 500;
  display: flex;
  align-items: center;
  justify-content: center;
}

.register-options {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-bottom: 20px;
  color: #606266;
}

.register-footer {
  font-size: 12px;
  color: #909399;
}

.register-decoration {
  position: absolute;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.floating-circle {
  position: absolute;
  border-radius: 50%;
  opacity: 0.6;
}

.circle-1 {
  width: 300px;
  height: 300px;
  background: linear-gradient(45deg, #409EFF, #53a8ff);
  top: -100px;
  right: -100px;
  animation: float 8s infinite ease-in-out;
}

.circle-2 {
  width: 200px;
  height: 200px;
  background: linear-gradient(45deg, #67C23A, #85ce61);
  bottom: -50px;
  left: -50px;
  animation: float 10s infinite ease-in-out reverse;
}

.circle-3 {
  width: 150px;
  height: 150px;
  background: linear-gradient(45deg, #E6A23C, #f3d19e);
  top: 60%;
  right: 15%;
  animation: float 12s infinite ease-in-out;
}

@keyframes float {
  0%, 100% {
    transform: translateY(0) scale(1);
  }
  50% {
    transform: translateY(-20px) scale(1.05);
  }
}

.animate-fade-in {
  animation: fadeIn 0.8s ease-out;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}
</style>